package com.ddj.bo;

import java.util.List;

import com.ddj.dao.Petdao;
import com.ddj.dao.UniversalDAO;
import com.ddj.po.Petpo;
import com.ddj.util.MyTime;

public class Petbo {
    public String login(Petpo po){
	String state="";
	Petdao dao = new Petdao();
	String hql = "select new Petpo(name,password) from Petpo" +
	" where name = :name and password= :password";
	
	if(dao.loginDo(po,hql)){
	    state="";
	}else{
	    state="登录失败，用户名或密码有误";
	}
	return state;
    }

    public String save(Petpo po) {
	String state="";
	MyTime time = new MyTime();
	po.setDate(time.getDate());   //存入宠物注册的时间 用于以后计算宠物年龄
	Petdao dao = new Petdao();
	if(dao.saveDo(po)){
	    state="注册成功了哦";
	}else{
	    state="";
	}
	return state;
    }
    
    public String modify(Petpo po) {
	String state="";
	Petdao dao = new Petdao();
	if(dao.saveDo(po)){
	    state="修改成功了哦";
	}else{
	    state="";
	}
	return state;
    }
    
    public Petpo query(String str,String value) {
	Petdao dao = new Petdao();
	Petpo petpo=new Petpo();
	petpo = dao.queryPet(str,value);
	return petpo;
    }
    public Petpo update(String name) {
	Petdao dao = new Petdao();
	Petpo petpo=new Petpo();
	return petpo;
    }
    
    /**
     * @param value 查询的值
     * @param flag 升序还是降序 true为升序，反之降序
     * @return
     */
    private List<Petpo> search(String hql,final String value,final Boolean flag){
	UniversalDAO udao = new UniversalDAO();
	if(!flag){
	    hql = hql + "desc";
	}
	String[] obj = new String[1];
	obj[0] = value;
    	List<Petpo> list = udao.find(hql, obj);
    	return list;
    }
    
    public List<Petpo> searchByType(Boolean flag){
	String hql = "from Petpo order by type";
	return search(hql,"",flag);
    }
    
    public List<Petpo> searchByPetName(String petName,boolean flag) {
    	String hql = "from Petpo where name like '%"+petName+"%' order by name";
    	return search(hql,petName,flag);
    }
    public List<Petpo> searchByHostName(String hostName,boolean flag) {
    	String hql = "from Petpo where hostName like '%"+hostName+"%' order by hostName";
    	return search(hql,hostName,flag);
    }
    

}
